home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / PlayerPRO 4.4.1 / Filters Plugs / Normalize.c < prev    next >
C/C++ Source or Header  |  1995-04-16  |  2KB  |  106 lines

  1. /*    Normalize        */
  2. /*    v 0.2            */
  3. /*    1995 by Liane    */
  4.  
  5. //    Usage:
  6. //    Another version of "Maximize", but this one
  7. //    will try to get the maximum gain.
  8. //    Works on the selected part or all the waveform
  9. //    if there is no selection.
  10.  
  11. #include "MAD.h"
  12. #include "PPPlug.h"
  13.  
  14. #if defined(powerc) || defined(__powerc)
  15. enum {
  16.         PlayerPROPlug = kCStackBased
  17.         | RESULT_SIZE(SIZE_CODE( sizeof(OSErr)))
  18.         | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof( Ptr*)))
  19.         | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof( struct FileInstrData*)))
  20.         | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof( long)))
  21.         | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof( long)))
  22.         | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof( PPInfoPlug*)))
  23. };
  24.  
  25. ProcInfoType __procinfo = PlayerPROPlug;
  26. #else
  27. #include <A4Stuff.h>
  28. #endif
  29.  
  30.  
  31. #define max(a,b) (((a) > (b)) ? (a) : (b))
  32. #define labs(a) (((a) < 0) ? (-a) : (a))
  33.  
  34. OSErr main(     Ptr                        *InstrumentPtr,
  35.                 struct FileInstrData    *theData,
  36.                 long                    SelectionStart,
  37.                 long                    SelectionEnd,
  38.                 PPInfoPlug                *thePPInfoPlug)
  39. {
  40. long    i, peak = 0, temp;
  41.  
  42.     if (SelectionStart == SelectionEnd) {
  43.         SelectionStart = 0;
  44.         SelectionEnd = theData->insSize;
  45.     }
  46.  
  47.     switch( theData->amplitude)
  48.     {
  49.         case 8:
  50.         {
  51.             Ptr    SamplePtr = (*InstrumentPtr) + SelectionStart;
  52.             for( i = 0; i < SelectionEnd - SelectionStart; i++)
  53.             {
  54.                 temp = *SamplePtr++;
  55.  
  56.                 peak = max (peak, labs(temp));
  57.             }
  58.             
  59.             if( peak != 0)
  60.             {
  61.                 peak = ((long)0x80 * 0x10000) / peak;
  62.                 
  63.                 SamplePtr = (*InstrumentPtr) + SelectionStart;
  64.                 for( i = 0; i < SelectionEnd - SelectionStart; i++)
  65.                 {
  66.                     temp = *SamplePtr;
  67.             
  68.                     temp = (peak * temp) / 0x10000;
  69.             
  70.                     if( temp > 127) temp = 127;
  71.                     else if( temp < -127 ) temp = -127;
  72.                     
  73.                     *SamplePtr++ = temp;
  74.                 }
  75.             }
  76.         } break;
  77.  
  78.         case 16:
  79.         {
  80.             short    *SamplePtr = (short*) *InstrumentPtr + (SelectionStart / 2);
  81.  
  82.             for( i = 0; i < (SelectionEnd - SelectionStart) / 2; i++)
  83.             {
  84.                 temp = (long)*SamplePtr++;
  85.                 peak = max (peak, labs(temp));
  86.             }
  87.             
  88.             if( peak != 0)
  89.             {
  90.                 peak = ((long)0x8000 * 0x10000) / peak;
  91.                 
  92.                 SamplePtr = (short*) *InstrumentPtr + (SelectionStart / 2);
  93.                 for( i = 0; i < (SelectionEnd - SelectionStart) / 2; i++)
  94.                 {
  95.                     temp = (long)*SamplePtr;
  96.             
  97.                     temp = -(peak * temp) / 0x10000;
  98.             
  99.                     *SamplePtr++ = temp;
  100.                 }
  101.             }
  102.         } break;
  103.     }
  104.     
  105.     return noErr;
  106. }